#include<cstdio>
#include<map>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=2010;
map<int,int>id2seq;
int M,N;
int seq=0;
struct data{
    int id;
    int score[4];//A,C,M,E
}Stu[maxn];
int idx;
bool cmp(data a,data b){
    return a.score[idx]>b.score[idx];
}
void findseq(int id){
    if(id2seq.find(id)==id2seq.end()){
        id2seq[id]=seq++;
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    char flag[4]={'A','C','M','E'};
    scanf("%d %d",&M,&N);
    for(int i=0;i<M;i++){
        scanf("%d %d %d %d",&Stu[i].id,&Stu[i].score[1],&Stu[i].score[2],&Stu[i].score[3]);
        findseq(Stu[i].id);
        Stu[i].score[0]=floor((Stu[i].score[1]+Stu[i].score[2]+Stu[i].score[3])/3);
    }
    for(int i=0;i<N;i++){
        int inpid;
        scanf("%d",&inpid);
        if(id2seq.find(inpid)==id2seq.end()){printf("N/A\n");continue;}
        data tmp[maxn];
        int maxrank=0x3f3f3f3f;
        int maxidx=0;
        for(int j=0;j<=3;j++){
            memcpy(tmp,Stu,sizeof(Stu));
            idx=j;sort(tmp,tmp+M,cmp);
            int seq=0;
            for(int k=0;k<M;k++){
                if(k>0&&tmp[k].score[idx]==tmp[k-1].score[idx]){
                    seq--;
                }
                else{
                    seq=0;
                }
                if(tmp[k].id==inpid){seq=seq+k;break;}
            }
            if(seq<maxrank){maxrank=seq;maxidx=j;}
        }
        printf("%d %c\n",maxrank+1,flag[maxidx]);

    }
}
